﻿using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompany.MyProject.Common.Caches
{
    /// <summary>
    /// 缓存抽象接口，基于 IDistributedCache 封装
    /// </summary>
    public interface ICaching
    {
        /// <summary>
        /// 微软官方分布式缓存接口
        /// </summary>
        public IDistributedCache Cache { get; }
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        void AddCacheKey(string cacheKey);
        /// <summary>
        /// 异步添加缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        Task AddCacheKeyAsync(string cacheKey);
        /// <summary>
        /// 根据模式删除
        /// </summary>
        /// <param name="key"></param>
        void DelByPattern(string key);
        /// <summary>
        /// 异步根据模式删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task DelByPatternAsync(string key);

        /// <summary>
        /// 根据建判断是否存在
        /// </summary>
        /// <param name="chcheKey"></param>
        /// <returns></returns>
        bool Exists(string chcheKey);
        /// <summary>
        /// 异步 根据建判断是否存在
        /// </summary>
        /// <param name="chcheKey"></param>
        /// <returns></returns>
        Task<bool> ExistsAsync(string chcheKey);
        /// <summary>
        /// 获取所有的缓存key
        /// </summary>
        /// <returns></returns>
        List<string> GetAllCacheKeys();
        /// <summary>
        /// 异步 获取所有的缓存key
        /// </summary>
        /// <returns></returns>
        Task<List<string>> GetAllCacheKeysAsync();
        /// <summary>
        /// 根据缓存键获取泛型类型的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        T Get<T>(string cacheKey);
        /// <summary>
        /// 异步 根据缓存健获取泛型对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        Task<T> GetAsync<T>(string cacheKey);
        /// <summary>
        /// 根据 类型和缓存key 获取object类型的缓存
        /// </summary>
        /// <param name="type"></param>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        object Get(Type type, string cacheKey);
        /// <summary>
        /// 异步 根据 类型和缓存key 获取object类型的缓存
        /// </summary>
        /// <param name="type"></param>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        Task<object> GetAsync(Type type, string cacheKey);
        /// <summary>
        /// 根据缓存key获取字符串类型的缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        string GetString(string cacheKey);
        /// <summary>
        /// 异步 根据缓存key获取字符串类型的缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        Task<string> GetStringAsync(string cacheKey);
        /// <summary>
        /// 根据缓存key 删除对应的缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        void Remove(string cacheKey);
        /// <summary>
        /// 异步 根据缓存key 删除对应的缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        Task RemoveAsync(string cacheKey);
        /// <summary>
        /// 删除全部缓存
        /// </summary>
        void RemoveAll();
        /// <summary>
        /// 异步 删除全部缓存
        /// </summary>
        /// <returns></returns>
        Task RemoveAllAsync();
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="cacheKey">缓存key</param>
        /// <param name="value">缓存值</param>
        /// <param name="expire">过期时间</param>
        void Set<T>(string cacheKey, T value, TimeSpan? expire = null);
        /// <summary>
        /// 异步设置缓存
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="cacheKey">缓存key</param>
        /// <param name="value">缓存值</param>
        /// <returns></returns>
        Task SetAsync<T>(string cacheKey, T value);
        /// <summary>
        /// 异步设置缓存，并带有过去时间
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="cacheKey">缓存key</param>
        /// <param name="value">缓存值</param>
        /// <param name="expire">过期时间</param>
        /// <returns></returns>
        Task SetAsync<T>(string cacheKey, T value, TimeSpan expire);
     
        void SetPermanent<T>(string cacheKey, T value);
        Task SetPermanentAsync<T>(string cacheKey, T value);

        void SetString(string cacheKey, string value, TimeSpan? expire = null);

        Task SetStringAsync(string cacheKey, string value);
        Task SetStringAsync(string cacheKey, string value, TimeSpan expire);

        Task DelByParentKeyAsync(string cacheKey);
    }
}
